package com.molyfun.parents.im.chatrow;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.coursecenter.ProductDetailActivity;
import com.molyfun.parents.im.model.ChatMessageEntity;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.utils.StringUtils;
import com.squareup.picasso.Picasso;

public class ChatRowProduct extends BaseChatRow{

//    private TextView contentView;

    /**
     * bigurl : http://media.molyfun.com/mf/product/pics/ic_20170629-150232985.jpg?imageMogr2/thumbnail/1280x1280
     * id : 402883e05ea869c9015ea86ca81a0000
     * planId : 691efe6342e647b3aa91aad92715781e
     * userId : e548cebc5df0285e015df453bfd908a5
     * userName : 预约咨询
     * userType : 2
     * headimgurl : 头像  http://media.molyfun.com/helloimg/user/20170907/201709071903384cJQnTjk.jpg?imageMogr2/thumbnail/300x300
     * content :    文字内容
     * url : http://media.molyfun.com/mf/product/pics/ic_20170629-150232985.jpg?imageMogr2/thumbnail/300x300
     * type : 5     记录类型   1:图片 2.视频 3.音频 4.文字 5.套餐  6.评价
     * timelen :    音频时长
     * cover :  视频封面
     * extendfield1 : 8a2d92635ceedfa6015cf2a889640b52  扩展字段1  （type=5时表示套餐Id  ；type=6时表示评价结果  1 满意  2 一般  3 不满意）
     * extendfield2 : 套餐    扩展字段2  套餐标题
     * extendfield3 : 2.00      扩展字段3  价格
     * extendfield4 : 0.10      扩展字段4  折扣价
     * extendfield5 : 2     扩展字段5  售出量
     * extendfield6 : 0     扩展字段6  库存
     * createdatetimeStr : 2017-09-22 15:10:47
     */

    private ImageView mVideoView;
    private TextView mProductTitle;
    private TextView mPriceNow;
    private TextView mPriceOld;
    private TextView mNickName;
    private boolean mShowNickName;

    public ChatRowProduct(Context context, ChatMessageEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    public ChatRowProduct(Context context, ChatMessageEntity message, int position, boolean showNickName, BaseAdapter adapter) {
        super(context, message, position, adapter);
        mShowNickName = showNickName;
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(R.layout.ease_row_received_product , this);
    }

    @Override
    protected void onFindViewById() {
//        contentView = (TextView) findViewById(R.id.tv_chatcontent);

        mVideoView = (ImageView) findViewById(R.id.product_cover);
        mProductTitle = (TextView) findViewById(R.id.product_title);
        mPriceNow = (TextView) findViewById(R.id.price_now);
        mPriceOld = (TextView) findViewById(R.id.price_old);
    }

    @Override
    public void onSetUpView() {
//        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
//        Spannable span = EaseSmileUtils.getSmiledText(context, message.getContent());//txtBody.getMessage());
        // 设置内容
//        contentView.setText(span, TextView.BufferType.SPANNABLE);

        if(!TextUtils.isEmpty(message.getUrl())){
            Picasso.with(context)
                    .load(message.getUrl())
                    .into(mVideoView);
        }
        mProductTitle.setText(message.getExtendfield2());
        mPriceNow.setText("￥" + message.getExtendfield4());

        mPriceOld.setText("￥" + message.getExtendfield3());
        mPriceOld.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线

        if(mShowNickName) {
            if(message.direct() == EMMessage.Direct.RECEIVE){
                mNickName.setText(StringUtils.getSpltString(message.getUsername(), 10));
            }else{
                mNickName.setText("[" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getStudentname(), 10) + "]" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getUsername(), 10));
            }
        }

        handleTextMessage();
    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
//                    progressBar.setVisibility(View.GONE);
//                    statusView.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
//                    progressBar.setVisibility(View.GONE);
//                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL:
//                    progressBar.setVisibility(View.GONE);
//                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS:
//                    progressBar.setVisibility(View.VISIBLE);
//                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }

    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub
//        ProductDetailActivity.startActivity(getContext(), message.getExtendfield1());
        ProductDetailActivity.startActivity(getContext(), message.getExtendfield1(), PreferencesStorageUtil.getStudentId(getContext()), Const.CHATROWPRODUCT);
    }
}
